<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <title>addMedicine</title>
    <link rel="stylesheet" type="text/css" th:href="@{/static/layui/css/layui.css}"/>
</head>
<body>
    <script th:src="@{/static/layui/layui.js}" type="text/javascript" charset="utf-8"></script>
    <script th:src="@{/static/jquery-2.1.4.js}" type="text/javascript" charset="utf-8"></script>
    <script th:src="@{/static/mylayer.js}" type="text/javascript" charset="utf-8"></script>
    <script th:src="@{/static/kindeditor/kindeditor.js}" type="text/javascript" charset="utf-8"></script>

    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">药品名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" autocomplete="off" placeholder="请输入药品名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">药品售价</label>
            <div class="layui-input-block">
                <input type="text" name="sellPrice" autocomplete="off" placeholder="请输入药品售价" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">所属类别</label>
            <div class="layui-input-block">
                <select id="categoryId" name="categoryId">
                    <option value="">请选择类别</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="submitDemo">添加</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

    <script>
        layui.use(['form'], function () {
            var form = layui.form;

            $(function () {
                $.post(
                    '/category/selectAll',
                    {'category':$(this).val()},
                    function (jsonObj) {
                        console.log(jsonObj);
                        $(jsonObj).each(function () {
                            $('#categoryId').append('<option value="' + this.id + '">' + this.name + '</option>');
                        })
                        form.render("select");
                    },
                    'json'
                )
            });

            //监听提交
            form.on('submit(submitDemo)', function(data){
                // layer.alert(JSON.stringify(data.field), {
                //     title: '最终的提交信息'
                // })
                console.log(data.field);
                $.post(
                    '/medicine/add',
                    data.field,
                    function (result) {
                        console.log(result);
                        if(result.code == 0) {
                            mylayer.okMsg(result.msg);
                            setInterval(function () {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                window.parent.location.reload();
                            }, 2000);
                        }
                    },
                    'json'
                )
                return false;
            });
        })

    </script>
    <a></a>
</body>
</html>
